package com.angel.provider.app.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Service;

import com.angel.core.Constants;
import com.angel.core.util.HttpRequest;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.MD5Util;
import com.angel.core.util.PropertyConfUtil;
import com.angel.provider.app.WangYiProvider;

/**
 * @author 作者姓名 cheng
 * @version 创建时间：2016年9月18日 下午4:48:26 类说明
 */
@Service
public class WangYiProviderImpl implements WangYiProvider {

	/**
	 * 创建云信用户
	 */
	@Override
	public String createUser(String json) throws Exception {
		// TODO Auto-generated method stub
		Map<String, String> map = JsonUtil.jsonStringToMap(json);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("accid", map.get("accid")));
		nvps.add(new BasicNameValuePair("name", map.get("name")));

		String url = PropertyConfUtil.getContextProperty(Constants.WYYX_APP_URL) + Constants.WYYX_APP_CREATE_USER;
		String post = HttpRequest.toPost(url, header(), nvps);
		return post;
	}

	/**
	 * 云信接口header设置
	 * 
	 * @return
	 */
	private Map<String, String> header() {
		String appKey = PropertyConfUtil.getContextProperty(Constants.WYYX_APP_KEY);
		String appSecret = PropertyConfUtil.getContextProperty(Constants.WYYX_APP_SECRET);
		String nonce = PropertyConfUtil.getContextProperty(Constants.WYYX_APP_NONCE);
		String curTime = String.valueOf((new Date()).getTime() / 1000L);
		Map<String, String> header = new HashMap<String, String>();
		header.put("AppKey", appKey);
		header.put("Nonce", nonce);
		header.put("CurTime", curTime);
		header.put("CheckSum", MD5Util.getCheckSum(appSecret, nonce, curTime));
		header.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		return header;
	}

	/**
	 * 创建云信群
	 */
	@Override
	public String createTeam(String json) throws Exception {
		// TODO Auto-generated method stub
		Map<String, String> map = JsonUtil.jsonStringToMap(json);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next().toString();
			String value = map.get(key);
			if ("mobile".equals(key)) {
				nvps.add(new BasicNameValuePair("members", "['_" + value + "']"));
				nvps.add(new BasicNameValuePair("owner", "_" + value));
			} else {
				nvps.add(new BasicNameValuePair(key, value));
			}
		}
		String url = PropertyConfUtil.getContextProperty(Constants.WYYX_APP_URL) + Constants.WYYX_APP_CREATE_TEAM;
		String post = HttpRequest.toPost(url, header(), nvps);
		return post;
	}

	/**
	 * 解散云信群
	 */
	public String removeTeam(String json) throws Exception {
		// TODO Auto-generated method stub
		Map<String, String> map = JsonUtil.jsonStringToMap(json);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next().toString();
			String value = map.get(key);
			if ("mobile".equals(key)) {
				nvps.add(new BasicNameValuePair("owner", "_" + value));
			} else {
				nvps.add(new BasicNameValuePair(key, value));
			}
		}
		String url = PropertyConfUtil.getContextProperty(Constants.WYYX_APP_URL) + Constants.WYYX_APP_REMOVE_TEAM;
		String post = HttpRequest.toPost(url, header(), nvps);
		return post;
	}

	/**
	 * 编辑云信群
	 */
	public String updateTeam(String json) throws Exception {
		// TODO Auto-generated method stub
		Map<String, String> map = JsonUtil.jsonStringToMap(json);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next().toString();
			String value = map.get(key);
			if ("mobile".equals(key)) {
				nvps.add(new BasicNameValuePair("owner", "_" + value));
			} else {
				nvps.add(new BasicNameValuePair(key, value));
			}
		}
		String url = PropertyConfUtil.getContextProperty(Constants.WYYX_APP_URL) + Constants.WYYX_APP_UPDATE_TEAM;
		String post = HttpRequest.toPost(url, header(), nvps);
		return post;
	}

	/**
	 * 更新云信
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public String updateUinfo(String json) throws Exception {
		Map<String, String> map = JsonUtil.jsonStringToMap(json);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next().toString();
			String value = map.get(key);
			switch (key) {
			case "mobile":
				nvps.add(new BasicNameValuePair("accid", "_" + value));
				nvps.add(new BasicNameValuePair("mobile", value));
				break;
			case "memPhone":
				nvps.add(new BasicNameValuePair("accid", value));
				nvps.add(new BasicNameValuePair("mobile", value));
				break;
			case "name":
				nvps.add(new BasicNameValuePair("name", value));
				break;
			case "portraitUrl":
				nvps.add(new BasicNameValuePair("icon", value));
				break;
			case "photo":
				nvps.add(new BasicNameValuePair("icon", value));
				break;
			case "email":
				nvps.add(new BasicNameValuePair("email", value));
				break;
			case "sex":
				nvps.add(new BasicNameValuePair("gender", value));
				break;
			default:
				break;
			}
		}
		String url = PropertyConfUtil.getContextProperty(Constants.WYYX_APP_URL) + Constants.WYYX_APP_UPDATE_USER;
		String post = HttpRequest.toPost(url, header(), nvps);
		return post;
	}

}
